<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 14:35
 */

namespace HiCommon\Service;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Cache;

class Clear extends BaseService
{

    /**
     * 清除各类缓存操作
     *
     * @param array $arr_input
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function clear_cache_operate(array $arr_input)
    {
        $arrData = $arr_input['clear'];
        foreach ($arrData as $value)
        {
            $res = Clear::clear_operate_by_type($value);
            if($res === false) {
                return hello_error('操作失败');
            }
        }
        return hello_success('清除成功');
    }

    /**
     * 根据类型清除缓存
     *
     * @param string $type
     *
     * @return bool
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function clear_operate_by_type(string $type)
    {
        switch ($type)
        {
            case 'view':
                Artisan::call('view:clear');
                return true;
                break;
            case 'route':
                Artisan::call('route:clear');
                return true;
                break;
            case 'cache':
                Artisan::call('cache:clear');
                return true;
                break;
            case 'config':
                Artisan::call('config:clear');
                return true;
                break;
            case 'menu':
                Cache::forget('menus:' . admin_user_id());
                return true;
                break;
            default:
                return false;
        }
    }
}